/**
 * 载入项目引导文件
 */
require('../bootstrap');

let box;

//增加
let add = (parent_id = 0) => {
    //加载父级菜单选择项
    //loadOption(parent_id);
    $("#box-form input[name='parent_id']").val(parent_id);
    $("#box-form form").resetForm();
    $("#box-form input[name='perm_id']").val(0);
    $("#box-form input[name='is_public'][value=0]").attr('checked', true);
    $("#box-form input[name='is_opentree'][value=1]").attr('checked', true);
    box = layer.open({
        type: 1,
        title: parent_id ? '添加子权限' : '添加权限',
        anim: 2,
        shadeClose: true, //开启遮罩关闭
        skin: 'layui-layer-molv', //加上边框
        area: ['620px', '600px'], //宽高
        content: $('#box-form')
    });
}
//修改
let edit = (id) => {
    $(".modal-title").html('编辑权限');
    utils.ajaxGet(page_params.url_get, {
        'id' : id,
    }, (response) => {
        let data = response.data;
        if (data.data == null){
            toastr.error('数据不存在', '错误');
            return false;
        }
        $("#box-form input[name='perm_id']").val(data.data.perm_id);
        $("#box-form input[name='parent_id']").val(data.data.parent_id);
        $("#box-form input[name='display_name']").val(data.data.display_name);
        $("#box-form input[name='name']").val(data.data.name);
        $("#box-form textarea[name='description']").val(data.data.description);
        var is_public = data.data.is_public;
        $("#box-form input[name='is_public'][value=" + is_public +"]").prop('checked', 'checked');
        var is_opentree = data.data.is_opentree;
        $("#box-form input[name='is_opentree'][value=" + is_opentree +"]").prop('checked', 'checked');
        //$("#box-form select[name='parent_id']").val(data.data.parent_id);
        //加载父级菜单选择项
        //loadOption(data.data.parent_id);
        box = layer.open({
            type: 1,
            title: '修改权限',
            anim: 2,
            shadeClose: true, //开启遮罩关闭
            skin: 'layui-layer-molv', //加上边框
            area: ['620px', '600px'], //宽高
            content: $('#box-form')
        });
    });
}
$(document).ready(function(){
    //加载树结构
    $('#jstree').jstree({
        'core' : {
            "multiple": false,
            'check_callback' : true,
            "themes" : {
                "variant" : "large"
            },
            'data' : {
                'url' : page_params.url_list,
                'data' : function (node) {
                    //return {'id' : node.id};
                }
            }
        },
        'types' : {
            'default' : {
                'icon' : 'fa fa-folder'
            },
        },
        "checkbox" : {
            "keep_selected_style" : false
        },
        "plugins" : [ 'types', 'dnd', 'wholerow','unique','contextmenu'],
        "contextmenu": {
            "items": {
                "create": null,
                "rename": null,
                "remove": null,
                "ccp": null,
                "add": {
                    "label": "添加子项",
                    "action": function (obj) {
                        let inst = jQuery.jstree.reference(obj.reference);
                        let clickedNode = inst.get_node(obj.reference);
                        add(clickedNode.id);
                    }
                },
                "edit": {
                    "label": "编辑",
                    "action": function (obj) {
                        let inst = jQuery.jstree.reference(obj.reference);
                        let clickedNode = inst.get_node(obj.reference);
                        edit(clickedNode.id);
                    }
                },
                "delete": {
                    "label": "删除",
                    "action": function (obj) {
                        let inst = jQuery.jstree.reference(obj.reference);
                        let clickedNode = inst.get_node(obj.reference);
                        utils.confirm(page_params.url_del,{'ids':[clickedNode.id]}, (response, postdata) =>{
                            data = response.data;
                            utils.showToastr(data);
                            if (data.status == 'success'){
                                //重新加载树结构
                                let tree = $.jstree.reference("#jstree");
                                tree.refresh();
                            }
                        });
                    }
                }
            }
        }
    });
    //移动事件
    $('#jstree').on('move_node.jstree', function(e,data){
        utils.ajaxPost(page_params.url_sort,{
            id : data.node.id,
            parent : data.parent,
            position:data.position
        }, (response, postdata) =>{
            data = response.data;
            utils.showToastr(data);
            if (data.status == 'success'){
                //重新加载树结构
                let tree = $.jstree.reference("#jstree");
                tree.refresh();
            }
        });
    })
    //表单
    /*$('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green',
    });*/
    $('#btn-sub').on('click', function () {
        utils.ajaxSubmitForm($("#box-form form"), '', {}, (response, postdata) =>{
            data = response.data;
            utils.showToastr(data);
            if (data.status == 'success'){
                layer.close(box);
                //重新加载树结构
                let tree = $.jstree.reference("#jstree");
                tree.refresh();
            }
        });
    });
    //弹窗
    $(".add").on('click', function () {
        add(0);
    });
    $(".addchild").on('click', function () {
        let ref = $('#jstree').jstree(true),
            sel = ref.get_selected();
        if(!sel.length) {
            toastr.error('请选中您想要添加子项的权限', '错误');
            return false;
        }
        add(sel[0]);
    });
    $(".edit").on('click', function () {
        let ref = $('#jstree').jstree(true),
            sel = ref.get_selected();
        if(!sel.length) {
            toastr.error('请选中您想要编辑的权限', '错误');
            return false;
        }
        edit(sel[0]);
    });
    $(".del").on('click', function () {
        let ref = $('#jstree').jstree(true),
            sel = ref.get_selected();
        if(!sel.length) {
            toastr.error('请选中您想要删除的权限', '错误');
            return false;
        }
        utils.confirm(page_params.url_del, {'ids':sel}, (response, postdata) =>{
            data = response.data;
            utils.showToastr(data);
            if (data.status == 'success'){
                //重新加载树结构
                let tree = $.jstree.reference("#jstree");
                tree.refresh();
            }
        });
    });
});